/** 
 * @author hannz
 * content:article
 * creatTime:2023年7月17日09:50:26
  */
const articleCate = require('../db/model/articleCate')
const express = require("express");
const cate = express.Router();
const { check, body, validationResult } = require('express-validator')

/**
 * @swagger
 * /api/cate/getAllcates:
 *   get:
 *     tags:
 *       - 文章分类
 *     description: 获取文章分类
 *     security:
 *      - CoreAPI: []        
 *     responses:
 *       200:
 *         description: 获取成功
 *
 */
cate.get('/getAllcates', (req, res) => {

    articleCate.findAll({
      
    }).then(data => {
        res.json({
            status: 200,
            message: '',
            data
        })
    })
})

/**
 * @swagger
 * /api/cate/addCate:
 *   post:
 *     tags:
 *       - 文章分类
 *     description: 添加分类
 *     security:
 *      - CoreAPI: []
 *     consumes:
 *     - "application/json"
 *     requestBody:
 *       required: true
 *       content:
 *          application/json:
 *              schema:
 *                  type: object
 *                  properties:
 *                      cateName:
 *                          type:string
 *                          description:分类名称
 *                      
 *              example:        #请求参数样例。
 *                      cateName: akaNum
 *          
 *                          
 *     responses:
 *       200:
 *         description: 添加成功
 *
 */
cate.post('/addCate', [
    check('cateName', '分类名称不能为空').notEmpty(),
], (req, res) => {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
        return res.json({
            status: 405,
            message: '请检查参数是否合法！',
            data: errors.array()
        })
    }
    const { cateName } = req.body
    articleCate.findAll({
        where: {
            cateName,
        }
    }).then(data => {
        if (data.length > 0) {
            res.json({
                status: 403,
                message: '该分类名已存在！',
            })
        } else {
            articleCate.create({
                cateName,
                create_time: new Date()
            }).then(cate => {
                res.json({
                    status: 200,
                    message: '添加成功！',
                })
            })
        }
    })
})
/**
 * @swagger
 * /api/cate/editCate:
 *   post:
 *     tags:
 *       - 文章分类
 *     description: 修改分类
 *     security:
 *      - CoreAPI: []
 *     consumes:
 *     - "application/json"
 *     requestBody:
 *       required: true
 *       content:
 *          application/json:
 *              schema:
 *                  type: object
 *                  properties:
 *                      cateName:
 *                          type:string
 *                          description:分类名
 *                      id:
 *                          type:number
 *                          description:分类id
 *                      
 *              example:        #请求参数样例。
 *                      cateName: test2
 *                      id: 1
 *                          
 *     responses:
 *       200:
 *         description: 修改成功
 *
 */
cate.post('/editCate', [
    check('cateName', '分类名称不能为空').notEmpty(),
    check('id', '作者id不能为空').notEmpty(),
], (req, res) => {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
        return res.json({
            status: 405,
            message: '请检查参数是否合法！',
            data: errors.array()
        })
    }
    const { id } = req.body
    articleCate.findByPk(id)
        .then(data => {
            if (data != null) {
                articleCate.update(req.body, {
                    where: {
                        id
                    }
                })
                    .then(data2 => {
                        res.json({
                            status: 200,
                            message: '修改成功！',
                        })
                    })
            } else {
                res.json({
                    status: 403,
                    message: 'id错误',
                })
            }
        })
})
/**
 * @swagger
 * /api/cate/delCate:
 *   post:
 *     tags:
 *       - 文章分类
 *     description: 删除分类
 *     security:
 *      - CoreAPI: []
 *     consumes:
 *     - "application/json"
 *     requestBody:
 *       required: true
 *       content:
 *          application/json:
 *              schema:
 *                  type: object
 *                  properties:
 *                      id:
 *                          type:number
 *                          description:id
 *                      
 *              example:        #请求参数样例。
 *                      id: 
 *                          
 *     responses:
 *       200:
 *         description: 删除成功
 *
 */
cate.post('/delCate', [
    check('id', '文章id不能为空').notEmpty(),
], (req, res) => {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
        return res.json({
            status: 405,
            message: '请检查参数是否合法！',
            data: errors.array()
        })
    }
    const { id } = req.body
    articleCate.findByPk(id)
        .then(data => {
            if (data != null) {
                articleCate.destroy({
                    where: {
                        id
                    }
                })
                    .then(data2 => {
                        res.json({
                            status: 200,
                            message: '删除成功！',
                        })
                    })
            } else {
                res.json({
                    status: 403,
                    message: 'id错误',
                })
            }
        })
})

module.exports = cate